package com.zzk.lanouhn.myapplication1.activity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.squareup.picasso.Picasso;
import com.zzk.lanouhn.myapplication1.R;
import com.zzk.lanouhn.myapplication1.adapters.InvestViewAdapter;
import com.zzk.lanouhn.myapplication1.fragments.AuthorsCollectFragment;
import com.zzk.lanouhn.myapplication1.fragments.AuthorsPublishFragment;
import com.zzk.lanouhn.myapplication1.modles.AuthorsInfo;
import com.zzk.lanouhn.myapplication1.utils.HttpUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者个人信息页面
 * Created by lanouhn on 16/9/10.
 */
public class AuthorsActivity extends AppCompatActivity implements View.OnClickListener {


    private TextView tv_name_author, tv_sign_author, tv_likes, tv_focuse, tv_fans;
    private ImageButton btn_back;
    private ImageView iv_logo;
    private Button btn_attention, btn_attented;
    private TabLayout tabs;
    private ViewPager viewpager;
    private List<String> tabNames;
    private List<Fragment> fragments;
    private AuthorsInfo authorsInfo;//作者信息实体类
    private String authorId;
    private InvestViewAdapter adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_original_author);
        initView();
        initData();
        initEvent();
        getAuthorInfoFromWeb();
    }

    private void initView() {
        tv_name_author = (TextView) findViewById(R.id.name_act_original_author);
        tv_sign_author = (TextView) findViewById(R.id.number_act_original_author);
        tv_likes = (TextView) findViewById(R.id.like_act_original_author);
        tv_focuse = (TextView) findViewById(R.id.attention_act_original_author);
        tv_fans = (TextView) findViewById(R.id.fans_act_original_author);

        iv_logo = (ImageView) findViewById(R.id.logo_act_original_author);

        btn_back = (ImageButton) findViewById(R.id.back_act_original_author);
        btn_attention = (Button) findViewById(R.id.guanzhu_act_original_author);
        btn_attented = (Button) findViewById(R.id.yiguanzhu_act_original_author);

        tabs = (TabLayout) findViewById(R.id.tabLayout_act_original_author);
        viewpager = (ViewPager) findViewById(R.id.viewPager_act_original_author);
    }

    private void initData() {
        tabNames = new ArrayList<>();
        tabNames.add("收藏");
        tabNames.add("发布");
        fragments = new ArrayList<>();
        AuthorsCollectFragment collectFragment = new AuthorsCollectFragment();
        fragments.add(collectFragment);

        AuthorsPublishFragment publishFragment = new AuthorsPublishFragment();
        fragments.add(publishFragment);

        adapter = new InvestViewAdapter(getSupportFragmentManager(), tabNames, fragments);
        viewpager.setAdapter(adapter);
        tabs.setupWithViewPager(viewpager);

        Bundle bundle = this.getIntent().getExtras();
        authorId = bundle.getString("authorId");
        SharedPreferences preferences=this.getSharedPreferences("user", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor=preferences.edit();
        editor.putString("authorId",authorId);
        editor.commit();
    }

    private void initEvent() {
        btn_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                authorId="";
                finish();
            }
        });
        btn_attented.setOnClickListener(this);
        btn_attention.setOnClickListener(this);
    }

    private void getAuthorInfoFromWeb() {
        String url = "http://open4.bantangapp.com/users/center/userInfo?";
        String infoUrl = "app_id=com.jzyd.BanTang&client_id=bt_app_android" +
                "&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d" +
                "&track_user_id=&oauth_token=&track_deviceid=863121028526879" +
                "&track_device_info=MI+2S&channel_name=xiaomi&app_installtime=1473128006" +
                "&app_versions=5.8.7&os_versions=5.0.2&screensize=720&v=19" +
                "&visit_user_id=" + authorId;
        try {
            HttpUtils.doPostAsyn(url, infoUrl, new HttpUtils.CallBack() {
                @Override
                public void onRequestComplete(String result) {
                    Gson gson = new Gson();
                    authorsInfo = gson.fromJson(result, AuthorsInfo.class);
                    if (authorsInfo.getData() != null) {
                        handler.sendEmptyMessage(1);
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:

                    break;
                case 1:
                    tv_name_author.setText(authorsInfo.getData().getNickname());
                    tv_sign_author.setText(authorsInfo.getData().getUser_sign());
                    if (!authorsInfo.getData().getPost_like().equals("")) {
                        tv_likes.setText(authorsInfo.getData().getPost_like());
                    }
                    if (!authorsInfo.getData().getAttentions().equals("")) {
                        tv_focuse.setText(authorsInfo.getData().getAttentions());
                    } else {
                        tv_focuse.setText("0");
                    }
                    if (!  authorsInfo.getData().getFans().equals("")) {
                        tv_fans.setText(authorsInfo.getData().getFans());
                    }

                    Picasso.with(AuthorsActivity.this).load(authorsInfo.getData().getAvatar()).into(iv_logo);
                    break;
                default:

                    break;
            }
        }
    };

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.guanzhu_act_original_author:
                Toast.makeText(this, "已关注", Toast.LENGTH_SHORT).show();
                btn_attented.setVisibility(View.VISIBLE);
                btn_attention.setVisibility(View.INVISIBLE);
                break;
            case R.id.yiguanzhu_act_original_author:
                Toast.makeText(this, "取消关注", Toast.LENGTH_SHORT).show();
                btn_attention.setVisibility(View.VISIBLE);
                btn_attented.setVisibility(View.INVISIBLE);
                break;
            default:

                break;
        }
    }
}
